package com.exer1.service;

import com.exer1.mapper.DormitoryMapper;
import com.exer1.pojo.Dormitory;
import com.exer1.utils.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @ClassName DormitoryService
 * @Description TODO
 * @Author Kx
 * @Date 2025/4/23 15:37
 * @Version 1.0
 */
public class DormitoryService {
    /**
     * @author: kx
     * @date: 2025/4/23 21:01
     * @param dormitory : 宿舍信息
     * @description: 添加宿舍信息
    */
    public void insertDormitory(Dormitory dormitory) {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            sqlSession.getMapper(DormitoryMapper.class).insertDormitory(dormitory);
        }
    }

    /**
     * @author: kx
     * @date: 2025/4/23 23:59
     * @param dormitory : 需要修改的宿舍信息
     * @description: 修改宿舍信息
    */
    public void updateDormitory(Dormitory dormitory) {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            sqlSession.getMapper(DormitoryMapper.class).updateDormitory(dormitory);
        }
    }

    /**
     * @param dormitoryId : 需要删除的宿舍编号
     * @author: kx
     * @date: 2025/4/24 0:11
     * @description: 删除宿舍信息
     */
    public void deleteDormitory(int dormitoryId) {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            sqlSession.getMapper(DormitoryMapper.class).deleteDormitory(dormitoryId);
        }
    }

    /**
     * @return java.util.List<com.exer1.pojo.Dormitory>
     * @author: kx
     * @date: 2025/4/24 0:19
     * @description: 查询所有宿舍信息
     */
    public List<Dormitory> findALLDormitory() {
        // 获取sqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            return sqlSession.getMapper(DormitoryMapper.class).findALLDormitory();
        }
    }

    /**
     * @author: kx
     * @date: 2025/4/24 11:17
     * @param dormitoryList : 宿舍信息集合
     * @description: 批量添加宿舍信息
     */
    public void batchInsert(List<Dormitory> dormitoryList) {
        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession(false);

        try {
            // 获取Mapper接口代理对象调用批量添加方法
            sqlSession.getMapper(DormitoryMapper.class).batchInsert(dormitoryList);

            // 提交事务
            sqlSession.commit();
        } catch (Exception e) {
            // 事物回滚
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            // 关闭sqlSession对象
            sqlSession.close();
        }
    }

    /**
     * @author: kx
     * @date: 2025/4/24 14:52
     * @param dormitoryId : 宿舍编号集合
     * @description: 批量删除宿舍信息
    */
    public void batchDelete(List<Integer> dormitoryId) {
        // 获取sqlSession对象
        SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession(false);

        try {
            // 获取Mapper接口代理对象调用批量删除方法
            sqlSession.getMapper(DormitoryMapper.class).batchDelete(dormitoryId);

            // 提交事务
            sqlSession.commit();
        } catch (Exception e) {
            // 事务回滚
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            // 关闭sqlSession对象
            sqlSession.close();
        }
    }
}
